﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Entity;

namespace DAL.Persistencia
{
    public class ServicoCompletoDAL
    {

        private NewTierraEntities Con;

        public ServicoCompletoDAL()
        {
            Con = new NewTierraEntities();
        }

        public void Salvar(Servico_Completo s)
        {
            try
            {
                Con.AddToServico_Completo(s);
                Con.SaveChanges();
            }
            catch 
            {                
                throw;
            }
        }

        public Servico_Completo ObterPorId(int IdServico)
        {
            try
            {
                return Con.Servico_Completo.Where(s => s.ServicoCompleto_id == IdServico).SingleOrDefault();
            }
            catch 
            {                
                throw;
            }
        }

        public Servico_Completo ObterPorIdTemp(int IdMServicoTemp)
        {
            try
            {
                return Con.Servico_Completo.Where(s => s.IdMServico_Temporada == IdMServicoTemp).First();
            }
            catch 
            {                
                throw;
            }
        }

        public Servico_Completo ObterPorIdMServicoTemp(int IdMServico, int IdMServicoTemp, int index)
        {
            try
            {
                return Con.Servico_Completo.Where(s => s.IdMServico == IdMServico &&
                                                       s.IdMServico_Temporada == IdMServicoTemp &&
                                                       s.Base_Index == index &&
                                                       s.SubServico == null).SingleOrDefault();
            }
            catch
            {
                throw;
            }
        }

        public List<Servico_Completo> ListarTodos()
        {
            try
            {
                return Con.Servico_Completo.ToList();
            }
            catch 
            {                
                throw;
            }
        }

        public List<Servico_Completo> ListarTodosTemporada(int IdMServicoTemp)
        {
            try
            {
                return Con.Servico_Completo.Where(s => s.IdMServico_Temporada == IdMServicoTemp).ToList();
            }
            catch 
            {
                throw;
            }
        }

        public List<Servico_Completo> ListarTodos(int IdServico)
        {
            try
            {
                return Con.Servico_Completo.Where(s => s.Servicos_Id == IdServico).ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<Servico_Completo> ListarTodosSubServico(int IdServico, int IdMServicoTemp)
        {
            try
            {
                return Con.Servico_Completo.Where(s => s.Servicos_Id == IdServico &&
                                                       s.IdMServico_Temporada == IdMServicoTemp &&
                                                       s.SubServico == null).ToList();
            }
            catch
            {
                throw;
            }
        }

        public object ListarTodosServPorSupplier(int IdSupplier)
        {
            try
            {

                return (from ssv in Con.S_Servicos
                        join scp in Con.Servico_Completo on ssv.Servicos_Id equals scp.Servicos_Id
                        join mon in Con.Monta_Servico on scp.IdMServico equals mon.IdMServico
                        where mon.S_id == IdSupplier
                        select new
                        {
                            ssv.Servicos_Nome,
                            mon.MServico_DataFrom,
                            mon.MServico_DataTo
                        }).Distinct().ToList();

            }
            catch
            {                
                throw;
            }
        }

        public void Excluir(Servico_Completo s)
        {
            try
            {
                Con.DeleteObject(s);
                Con.SaveChanges();
            }
            catch 
            {                
                throw;
            }
        }

        public void Atualizar(Servico_Completo novo)
        {
            try
            {
                Servico_Completo antigo = ObterPorId(novo.ServicoCompleto_id);

                antigo.ServicoCompleto_de = novo.ServicoCompleto_de;
                antigo.ServicoCompleto_ate = novo.ServicoCompleto_ate;

                Con.SaveChanges();
            }
            catch 
            {                
                throw;
            }
        }

        public bool VerificaMServicoTemp(int IdMServico, int IdMServicoTemp)
        {
            try
            {
                return Con.Servico_Completo.Where(s => s.IdMServico == IdMServico &&
                                                       s.IdMServico_Temporada == IdMServicoTemp).Count() != 0;
            }
            catch 
            {                
                throw;
            }
        }        

        public bool VerificaMServico(int IdMServico, int IdServico)
        {
            try
            {
                return Con.Servico_Completo.Where(s => s.IdMServico == IdMServico &&
                                                       s.Servicos_Id == IdServico).Count() != 0;
            }
            catch
            {
                throw;
            }
        }

    }
}
